package com.lyh.hkcloud.module.m1300026.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Parcelable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import com.lyh.hkcloud.R;
import com.lyh.hkcloud.constants.BusinessConstants;
import com.lyh.hkcloud.module.m1300026.IndexActivity;
import com.lyh.hkcloud.util.base.JsonUtil;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: zhongfan
 * @program: hkCloud
 * @description: 数据适配器
 * @company—date: 利元数据 2022-02-14 15:12
 **/

public class ListViewAdapter extends BaseAdapter {

    private Context context;
    private List<HashMap<String,Object>> dataList;
    //private int saveposition;
    //private DeleteCallback Callback;
    private IndexActivity mContext;

    /**
     * 构造器
     *
     * @param context  上下文对象
     * @param dataList 数据
     */
    public ListViewAdapter(Context context, List<HashMap<String,Object>> dataList) {
        this.context = context;
        this.dataList = dataList;
    }

    @Override
    public int getCount() {
        return dataList.size();
    }

    @Override
    public Object getItem(int position) {
        return dataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if(convertView == null){
            convertView = LayoutInflater.from(context).inflate(R.layout.activity_listview_item_1300026,parent,false);
            holder = new ViewHolder();
            holder.et_bill_number = (TextView) convertView.findViewById(R.id.tv_bill_number);
            holder.et_bill_statename = (TextView) convertView.findViewById(R.id.tv_bill_statename);
            holder.et_createtime = (TextView) convertView.findViewById(R.id.tv_createtime);
            holder.ll_details = convertView.findViewById(R.id.ll_details);

            convertView.setTag(holder);   //将Holder存储到convertView中
        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        holder.et_bill_number.setText(dataList.get(position).get("billnumber").toString());
        holder.et_bill_statename.setText(dataList.get(position).get("billstatename").toString());
        holder.et_createtime.setText(dataList.get(position).get("createtime").toString());

        holder.ll_details.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                Log.v("ll_details","点击了一下"+ position);
                Map<String, Object> inventoryMap = new HashMap<>();
                inventoryMap.put(BusinessConstants.INVENTORY_ID,dataList.get(position).get("billnumber").toString());
                inventoryMap.put(BusinessConstants.INVENTORY_STATUS,dataList.get(position).get("billstateid").toString());
                inventoryMap.put(BusinessConstants.BILL_TYPE,dataList.get(position).get(BusinessConstants.BILL_TYPE).toString());
                inventoryMap.put(BusinessConstants.ID,dataList.get(position).get(BusinessConstants.ID).toString());
                Intent intent = new Intent(context, com.lyh.hkcloud.module.m1300027.IndexActivity.class);
                intent.putExtra(BusinessConstants.DATA, JsonUtil.toJSONObject(inventoryMap).toString());
                context.startActivity(intent);
            }
        });

        return convertView;
    }

    /**
     * ViewHolder类
     */
    private final class ViewHolder {
        //盘点单号
        TextView et_bill_number;
        //盘点状态
        TextView et_bill_statename;
        //盘点时间
        TextView et_createtime;

        LinearLayout ll_details;

    }
}

